/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package healthcare.coursework.service;

import healthcare.coursework.model.Facility;
import healthcare.coursework.model.FacilityStatus;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless(name="FacilityService")
//@WebService
public class FacilityServiceImpl implements FacilityService {

    @PersistenceContext
    EntityManager em;

    //@WebMethod
    public List<Facility> listOfFacilities() {

        Query query=em.createQuery("SELECT f FROM Facility f WHERE f.isDeleted ='FALSE'");
        return query.getResultList();
    }

      //@WebMethod
    public List<Facility> listOfFacilities(String name) {

        Query query=em.createQuery("SELECT f FROM Facility f WHERE f.facilityName like ?2  AND f.isDeleted ='FALSE'");
        query.setParameter(2,"%"+ name+"%");
        return query.getResultList();
    }

    //@WebMethod
    public Facility findFacility(Long facilityId) {
        Query query=em.createQuery("SELECT f FROM Facility f WHERE f.facilityId =?1  AND f.isDeleted ='FALSE'");
        query.setParameter(1, facilityId);
        return (Facility) query.getSingleResult();
    }

    //@WebMethod
    public void updateFacility(Facility facility) {
        em.merge(facility);
    }

    //@WebMethod
    public void addFacility(String facilityName, String type, String description) {
        Facility facility=new Facility(facilityName, FacilityStatus.bookable, type, description);
        em.persist(facility);
    }

    //@WebMethod
    public void remove(Long facilityId) {
        Facility facility=em.find(Facility.class, facilityId);
         if (facility == null) {
            throw new EntityNotFoundException("cannot found this facility");
        }
        facility.setIsDeleted(true);
        em.merge(facility);
    }

    //@WebMethod
    public void recover(Long facilityId) {
        Facility facility=em.find(Facility.class, facilityId);
         if (facility == null) {
            throw new EntityNotFoundException("cannot found this facility");
        }
        facility.setIsDeleted(false);
        em.merge(facility);
    }

    //@WebMethod
    public void removeForever(Long facilityId) {
        em.remove(facilityId);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
